/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Service
*        文件名:             BusinessCardService.svc.cs
*        创建系统时间:       2012/11/2 19:07:04
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Service
{
    using System;
    using System.Data;
    using System.Reflection;
    using System.ServiceModel;
    using ECP.DataAccess;
    using ECP.DataAccess.Utilities;
    using ECP.Interface;
    using ECP.Model;
    using ECP.Utilities;

    [ServiceBehavior(Namespace = "http://jiangguang.net.cn/")]
    public partial class BusinessCardService : MarshalByRefObject, IBusinessCardService
    {
        private static BusinessCardService instance = null;
        private static object locker = new object();

        public string Add(BaseUserInfo userInfo, DataTable dataTable, out string statusCode, out string statusMessage)
        {
            BaseBusinessCardEntity businessCardEntity = new BaseBusinessCardEntity(dataTable);
            return this.AddEntity(userInfo, businessCardEntity, out statusCode, out statusMessage);
        }

        public string AddEntity(BaseUserInfo userInfo, BaseBusinessCardEntity businessCardEntity, out string statusCode, out string statusMessage)
        {
            string str = string.Empty;
            statusCode = string.Empty;
            statusMessage = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseBusinessCardDao dao = new BaseBusinessCardDao(dbHelper, userInfo);
                str = dao.Add(businessCardEntity);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return str;
        }

        public int BatchDelete(BaseUserInfo userInfo, string[] ids)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                num = new BaseBusinessCardDao(dbHelper, userInfo).BatchDelete(ids);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int BatchSave(BaseUserInfo userInfo, DataTable dataTable)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseBusinessCardDao dao = new BaseBusinessCardDao(dbHelper, userInfo);
                BaseBusinessCardEntity businessCardEntity = null;
                foreach (DataRow row in dataTable.Rows)
                {
                    businessCardEntity = new BaseBusinessCardEntity(row);
                    num += dao.Update(businessCardEntity);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public DataTable Get(BaseUserInfo userInfo, string id)
        {
            DataTable table = new DataTable(BaseBusinessCardTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseBusinessCardDao(dbHelper, userInfo).Get(id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public DataTable GetDT(BaseUserInfo userInfo)
        {
            DataTable dT = new DataTable(BaseBusinessCardTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dT = new BaseBusinessCardDao(dbHelper, userInfo).GetDT();
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public DataTable GetDTByUser(BaseUserInfo userInfo)
        {
            DataTable dT = new DataTable(BaseBusinessCardTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dT = new BaseBusinessCardDao(dbHelper, userInfo).GetDT(BaseBusinessCardTable.FieldCreateUserID, userInfo.ID);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public BaseBusinessCardEntity GetEntity(BaseUserInfo userInfo, string id)
        {
            return new BaseBusinessCardEntity(this.Get(userInfo, id));
        }

        public DataTable GetPublicDT(BaseUserInfo userInfo)
        {
            DataTable dT = new DataTable(BaseBusinessCardTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dT = new BaseBusinessCardDao(dbHelper, userInfo).GetDT(BaseBusinessCardTable.FieldPersonal, "0");
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public void Load()
        {
        }

        public int Update(BaseUserInfo userInfo, DataTable dataTable, out string statusCode, out string statusMessage)
        {
            BaseBusinessCardEntity businessCardEntity = new BaseBusinessCardEntity(dataTable);
            return this.UpdateEntity(userInfo, businessCardEntity, out statusCode, out statusMessage);
        }

        public int UpdateEntity(BaseUserInfo userInfo, BaseBusinessCardEntity businessCardEntity, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseBusinessCardDao dao = new BaseBusinessCardDao(dbHelper, userInfo);
                num = dao.Update(businessCardEntity);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public static BusinessCardService Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new BusinessCardService();
                        }
                    }
                }
                return instance;
            }
        }
    }
}